package com.asion.qkan.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.asion.qkan.R;
import com.asion.qkan.adapters.TuijianItemAdapter;
import com.asion.qkan.db.Cartoon;
import com.asion.qkan.jiguangtuisong.ExampleApplication;
import com.facebook.drawee.view.SimpleDraweeView;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by gino on 16-10-17.
 */

public class CollectioAdapter extends RecyclerView.Adapter<CollectioAdapter.ViewHolder> {
    private Context context;
    private List<Cartoon> list;
    private onitemclicklistener onitemclicklistener;

    public CollectioAdapter(Context context) {
        this.context = context;
        list = new ArrayList<>();
    }
    public void setItemClickListener(CollectioAdapter.onitemclicklistener onitemclicklistener){
        this.onitemclicklistener = onitemclicklistener;
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.category_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Cartoon cartoon = list.get(position);
        holder.image.setImageURI(cartoon.getImage());
        holder.title.setText(cartoon.getTitle());
        holder.author.setText(cartoon.getAuthor());
        holder.rela.setOnClickListener(v ->onitemclicklistener.onitemlistener(cartoon));
    }
    public void remove(Cartoon cartoon){
        list.remove(cartoon);
        notifyDataSetChanged();
    }

    public void removeAll(Collection collection){
        list.clear();
        notifyDataSetChanged();
    }
    public void addAll(Collection collection){
        list.addAll(collection);
        notifyDataSetChanged();
    }
    @Override
    public int getItemCount() {
        return list.size();
    }
    public interface onitemclicklistener{
        void onitemlistener(Cartoon cartoon);
    }
    public static class ViewHolder extends RecyclerView.ViewHolder{
        @BindView(R.id.category_item_imageview)
        SimpleDraweeView image;
        @BindView(R.id.category_item_title)
        TextView title;
        @BindView(R.id.category_item_author)
        TextView author;
        @BindView(R.id.category_item_watch)
        ImageView watch;
        @BindView(R.id.category_item_relative)
        RelativeLayout rela;
        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
            watch.setVisibility(View.INVISIBLE);
        }
    }
}
